package com.cmcc.aregister.ui.activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.Arguments;

/**
 * 语音挂号（临时）
 * @author ld
 * 
 */
@SuppressLint("SetJavaScriptEnabled")
public class VoiceWebviewActivity extends ParentActivity {
    // ------view------
    private Button top_left_image;

    // ------parameter------
    private int currentTitleSelect = -1;
    private boolean hasTwoTop = true;

    private WebView webView;
    private String url = "";
    private ProgressDialog proDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init(R.layout.p_voice_webview);
        mHttpManager.setCxt(getParent());
    }

    @Override
    protected void setupData() {
        hasTwoTop = !getIntent().hasExtra(Arguments.ACTIVITY_JUMP_SCREENING_TYPE);
        url = getIntent().getStringExtra(Arguments.ACTIVITY_JUMP_VOICE_RSP_URL);
    }

    @Override
    protected void setupView() {
//        ((TextView) findViewById(R.id.title)).setText(R.string.voice_register);
        top_left_image = (Button) findViewById(R.id.top_left_image);
        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (proDialog != null) {
                    proDialog.dismiss();
                    proDialog = null;
                }
                proDialog = ProgressDialog.show(VoiceWebviewActivity.this, null, getString(R.string.load_wap), true, false);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (proDialog != null) {
                    proDialog.dismiss();
                    proDialog = null;
                }
            }
        });
        // cookie管理
        CookieSyncManager cookieManager = CookieSyncManager.createInstance(this);
        CookieManager cm = CookieManager.getInstance();
        cm.removeAllCookie();
        // 开始和webView同步
        cookieManager.startSync();
        // 进行同步操作
        cookieManager.sync();
        cookieManager.stopSync();
        webView.loadUrl(url);
        if (hasTwoTop) {
            top_left_image.setVisibility(View.VISIBLE);
            top_left_image.setOnClickListener(onClickListener);
        }
    }

    public void check(int id, boolean hasAnimation) {
        if (currentTitleSelect == id) {
            return;
        }
        if (currentTitleSelect > 0) {
            findViewById(currentTitleSelect).setSelected(false);
        }
        currentTitleSelect = id;
        findViewById(currentTitleSelect).setSelected(true);
    }

    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
            case R.id.top_left_image:
                Intent intent = new Intent(VoiceWebviewActivity.this, NewIndexActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
                break;
            }
        }
    };

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
    }

}